<?php
    class DataioAction extends SCAction{
	  public function doout(){
	    $filename=$_SERVER['DOCUMENT_ROOT']."/databack/".date("Y_m_d-H_i_s-",time()).mt_rand(111111, 999999).".data";
		$this->mtbout($filename,array(
		"index",
		"info",
		"info_index",
		"menu",
		"products",
		"products_index"
		));

		header("Content-Type: application/force-download");
		header("Content-Disposition: attachment; filename=".basename($filename)); 
		readfile($filename);
		unlink($filename);
	  } 
	  public function doin(){
import('ORG.Net.UploadFile');
$upload = new UploadFile();// 实例化上传类
$upload->maxSize  = 3145728 ;// 设置附件上传大小
$upload->allowExts  = array('data');// 设置附件上传类型
$upload->savePath =  $_SERVER['DOCUMENT_ROOT'].'/databack/';// 设置附件上传目录
if(!$upload->upload()) {// 上传错误提示错误信息
$this->error($upload->getErrorMsg());
}else{// 上传成功 获取上传文件信息
$info =  $upload->getUploadFileInfo();
$this->doindata($info[0]['savename']); 
unlink($upload->savePath.$info[0]['savename']);
$this->success('数据导入成功！');
}
	  }
	  public function doindata($f){
	  $file = fopen($_SERVER['DOCUMENT_ROOT']."/databack/".$f,"r");
	  $a=fread($file,1);
	  $fs='nt';
	  while(!feof($file)){
	 	 if($fs=='nt'){
	  			if($a=='t'){
/*					if($rec!=NULL){
						foreach($rec as $dataset){
							foreach($dataset as $key=>$val){
								echo $key.'=>'.$val;
							}
						}
						$rec=NULL;
					}*/
					if($rec!=NULL){
						$this->dorec($tbname,$rec);
						$rec=NULL;
					}
					$fs='tl';
					$a=fread($file,1);
					$relen=$a;
				}elseif($a=='f'){
					$fs='fl';
					$a=fread($file,1);
					$relen=$a;				
				}elseif($a=='r'){
					$fs='rl';
					$a=fread($file,1);
					$relen=$a;	
				//	echo '<br>a:'.$a.'<br>';			
				}
				
	  	 }
	 	 elseif($fs=='tl'){
		 		$a=fread($file,1);
				if($a=='s'){
					//表名处理
					$tbname=fread($file,$relen);
					$fieldnl=0;
					$fieldns[0]='';
					$recindex=0;
					$fieldindex=0;
					$rec=NULL;
					//回归初始
					$fs='nt';
					$a=fread($file,1);
				}
				else{
					$relen=$relen.$a;
				}
		 }
		 elseif($fs=='fl'){
		 		$a=fread($file,1);
				if($a=='s'){
					//字段名处理
					$fieldns[$fieldnl]=fread($file,$relen);
					$fieldnl=$fieldnl+1;
					//回归初始
					$fs='nt';
					$a=fread($file,1);
				}
				else{
					$relen=$relen.$a;
				}
		 }
		 elseif($fs=='rl'){
		 		$a=fread($file,1);
				if($a=='s'){
					//字段处理
					$rec[$recindex][$fieldns[$fieldindex]]=fread($file,$relen);
					$fieldindex=$fieldindex+1;
					if($fieldindex>=$fieldnl){
						$fieldindex=0;// echo '('.$fieldindex.'):';
						$recindex=$recindex+1;// echo $recindex.'<br>';
					}
					//回归初始
					$fs='nt';
					$a=fread($file,1);
					//echo 'recindex:'.$recindex;
					//echo 'fieldindex:'.$fieldindex;
					//echo 'relen:'.$relen;
					//echo 'a:'.$a.'<br>';
				}
				else{
					$relen=$relen.$a;
				}
		 }
	  }
					if($rec!=NULL){
						$this->dorec($tbname,$rec);
						$rec=NULL;
					}
/*					if($rec!=NULL){
						foreach($rec as $dataset){
							foreach($dataset as $key=>$val){
								echo $key.'=>'.$val.',';
							}
						}
						$rec=NULL;
					}*/
	  fclose($file);
	  } 
	  public function showinfo(){
	     $datainfo=D('imginfo');
		       $this->imginfo_count=$datainfo->count();
	     $datainfo=D('imginfo_index');
		       $this->imginfo_index_count=$datainfo->count();
	     $datainfo=D('index');
		       $this->index_count=$datainfo->count();
	     $datainfo=D('info');
		       $this->info_count=$datainfo->count();
	     $datainfo=D('info_index');
		       $this->info_index_count=$datainfo->count();
	     $datainfo=D('menu');
		       $this->menu_count=$datainfo->count();
	     $datainfo=D('pics');
		       $this->pics_count=$datainfo->count();
	     $datainfo=D('pics_index');
		       $this->pics_index_count=$datainfo->count();
	     $datainfo=D('products');
		       $this->products_count=$datainfo->count();
	     $datainfo=D('products_index');
		       $this->products_index_count=$datainfo->count();
		 $this->display();
	  }
	private function dorec($tbname,$rec){
				$tab = M($tbname); 
				$tab->addAll($rec);
				//	echo $tbname.':';
				//	print_r($rec);
				//	echo '<br>';
	}
	protected function mtbout($f,$tables){
			$file = fopen($f,"wb");
			foreach($tables as $tb){
			    $datainfo=D($tb);
				$fields=$datainfo->getDbFields();
       			fwrite($file,"t".strlen($tb)."s".$tb);
				foreach($fields as $key){
					fwrite($file,"f".strlen($key).'s'.$key);
				}				
				foreach($datainfo->select() as $dataset){
					foreach($fields as $key){
						fwrite($file,"r".strlen($dataset[$key]).'s'.$dataset[$key]);
					}
				}	
			}
			fclose($file);
		}	  
	}

?>
